﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MotionIoLib;
using CommonTools;
using System.Threading;
using UserData;
using Sunny.UI;
using System.Threading;
namespace StationDemo
{
    public class UserDefineFunction
    {
        public static void Axis_回零(int axisID, string axisName, Stationbase station, bool bmanual = true)
        {
            if (station == null || axisID < 0)
                return;
            MotionMgr.GetInstace().ServoOn((short)axisID);
            MotionMgr.GetInstace().StopAxis((short)axisID);
            Thread.Sleep(300);
            if (MotionMgr.GetInstace().GetHomeFinishFlag(axisID) != AxisHomeFinishFlag.Homeing)
            {
                MotionMgr.GetInstace().SetAxisHomeingFlag(axisID);
                WaranResult result = station.HomeSigleAxisPosWaitInpos(axisID, station, 120000, bmanual);
                if (result != WaranResult.Run)
                {
                    throw new Exception($"{station.Name}{axisName}轴回零失败");
                }
                LogHelper.OnTest_ShowLog(station.Name, $"{station.Name}：{axisName}轴开始成功");
                MotionMgr.GetInstace().SetAxisHomeFinishFlag(axisID);
            }
        }

        public static bool Check_Axis_回零(int axisID)
        {
            if (axisID != -1)
                return MotionMgr.GetInstace().GetHomeFinishFlag(axisID) == AxisHomeFinishFlag.Homed;
            else
                return true;
        }

        public static bool Check_AllAxis_回零()
        {
            if (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.急停.ToString(),false,100))
            {
                MessageBox.Show("设备急停，不允许手动运行！", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                return false;
            }
            if (GlobalVariable.g_StationState == StationState.StationStatePause || GlobalVariable.g_StationState == StationState.StationStateRun)
            {
                MessageBox.Show("当前程序在运行，不允许手动运行！请先停止", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                return false;
            }
            if (GlobalVariable.g_StationState != StationState.StationStateStop || GlobalVariable.g_StationState == StationState.StationStateRun)
            {
                MessageBox.Show("设备未回零！请先停止", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                return false;
            }
            foreach (var name in Enum.GetNames(typeof(Station)))
            {
                Stationbase station = StationMgr.GetInstance().GetStation(name);
                if (!Check_Axis_回零(station.AxisX) || !Check_Axis_回零(station.AxisY) || !Check_Axis_回零(station.AxisZ) ||
                    !Check_Axis_回零(station.AxisTx) || !Check_Axis_回零(station.AxisU))
                {
                    MessageBox.Show($"{station.Name}不是所有轴都回到原点，不允许手动运行！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                    return false;
                }
            }
            return true;
        }
    }
}